#include<bits/stdc++.h>
#define ll long long
#define int ll
#define L x<<1
#define R L|1
#define mid (l+r>>1)
#define lc L,l,mid
#define rc R,mid+1,r
#define OK Ll<=l&&r<=Rr
#define Root 1,1,n
#define rep(x,y,z) for(int x=(y);x<=(z);x++)
#define per(x,y,z) for(int x=(y);x>=(z);x--)
#define repn(x) rep(x,1,n)
#define repm(x) rep(x,1,m)
#define pb push_back
#define e(x) for(int i=h[x],y=to[i];i;i=nxt[i],y=to[i])
#define E(x) for(auto y:p[x])
#define Pi pair<int,int>
#define ui unsigned ll
inline int read(){int s=0,w=1;char c=getchar();while(c<48||c>57) {if(c=='-') w=-1;c=getchar();}while(c>=48&&c<=57)s=(s<<1)+(s<<3)+c-48,c=getchar();return s*w;}
inline void pf(int x){if(x<0) putchar('-'),x=-x;if(x>9)pf(x/10);putchar(x%10+48);}
using namespace std;
const int N =8e3+5,M=6e4+5,inf=(1LL<<31)-1,mod=998244853;
const ll llf=1e18;
inline void add(int &a,int b){((a+=b)>=mod) and (a-=mod);}
inline int Add(int a,int b){return add(a,b),a;}
inline int mul(int a,int b){return 1LL*a*b%mod;}
inline void Mul(int &a,int b){a=mul(a,b);}
inline void red(int &a,int b){add(a,mod-b);}
inline int Red(int a,int b){return red(a,b),a;}
inline int qp(int a,int b){if(!b)return 1;int c=qp(a,b>>1);Mul(c,c);if(b&1)Mul(c,a);return c;}
inline int INV(int x){return qp(x,mod-2);}
int n,m;
struct node{
	int x,y,k;
}a[N];
bool vis[M];
int p[M],cnt,tot;
inline void prep(){
	rep(i,2,M-5){
		if(!vis[i])p[++cnt]=i;
		for(int j=1;j<=cnt&&p[j]*i<=M-5;j++){
			vis[i*p[j]]=1;
			if(i%p[j]==0)break;
		}
	}
	rep(i,1,M-5)vis[i]=0;
	rep(i,1,cnt)if(p[i]<=10||p[i]%20==3)vis[p[i]]=1,tot++;
}
inline int dist(int x,int y){
	return abs(a[x].x-a[y].x)+abs(a[x].y-a[y].y);
}
inline int medi(int x,int y,int z){
	return x+y+z-max({x,y,z})-min({x,y,z});
}
ll ans;
inline void sub0(){
	repn(i)rep(j,i+1,n)rep(k,j+1,n){
		if(vis[medi(dist(i,j),dist(j,k),dist(i,k))])ans+=a[i].k+a[j].k+a[k].k;
	}
	cout <<ans<<'\n';
}
inline void sub1(){
	ll sum2=0,sum3=0;
	repn(i)rep(j,i+1,n)if(vis[dist(i,j)]){
		repn(k)if(k!=i&&k!=j){
			if(medi(dist(i,j),dist(i,k),dist(k,j))==dist(i,j)){
				int ct=(dist(i,k)==dist(i,j))+(dist(j,k)==dist(i,j)),w=a[i].k+a[j].k+a[k].k;
				ans+=w;
				if(ct==2)sum3+=w;
				if(ct==1)sum2+=w;
			}
		}
	}
	cout <<ans-sum2/2-sum3/3*2<<'\n';
}
inline void Main(){
	n=read(),m=read(),ans=0;
	repn(i)a[i].x=read(),a[i].y=read();
	repn(i)a[i].k=read();
	if(n<=100)return sub0(),void();
	sub1();
}
signed main(){
	freopen("chess.in","r",stdin);
	freopen("chess.out","w",stdout);
	prep();
	int T=read();
	while(T--)Main();
	return 0;
}